<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">
<style>
    .suminfo{padding-top: 20px;font-weight: bold}
    .suminfo span{margin: 0 5px}
</style>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>销售方信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>销售方联系人</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="lianer" class="layui-input lianer" onclick="openPage('../public/choose_dealcom_lianer.php?dom=lianer&page=contract_chu','80%','80%')">
                            <div class="choose_div" id="lianer" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_dealcom_lianer.php?dom=lianer&page=contract_chu','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">销售方联系电话</label>
                        <div class="layui-input-block">
                            <input type="text" id="liantel" placeholder="请输入" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>



            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>合同信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>合同名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>合同编号</label>
                        <div class="layui-input-block">
                            <input type="text" name="number" id="number" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>采购类型</label>
                        <div class="layui-input-block">
                            <div id="caitype"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>合同类型</label>
                        <div class="layui-input-block">
                            <select id="hetype" name="hetype" lay-filter="hetype"></select>
                            <p class="error" id="hetype_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>合同金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" placeholder="请输入" autocomplete="off" class="layui-input moneyinput">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同份数</label>
                        <div class="layui-input-block">
                            <input type="number" name="henum" id="henum" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">发票类型</label>
                        <div class="layui-input-block">
                            <div id="qianpiaotype"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">税率(%)</label>
                        <div class="layui-input-block">
                            <input type="number" name="rate" id="rate" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>供应方(乙方)</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="gonger" class="layui-input gonger" onclick="choose_dealcom()">
                            <div class="choose_div" id="gonger" key="1"></div>
                            <span class="choose_one" onclick="choose_dealcom()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>购买方(甲方)</label>
                        <div class="layui-input-block">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=caier&type=dept&dept=chan&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="caier"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=caier&type=dept&dept=chan&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="caier_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>签订状态</label>
                        <div class="layui-input-block">
                            <select id="state" name="state" lay-filter="state" ></select>
                            <p class="error" id="state_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">签订日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="qiandt" id="qiandt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">签订人</label>
                        <div class="layui-input-block">
                            <input type="text" name="myer" id="myer" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联流程</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                            <div class="choose_div" id="flowrelas" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="advanersDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预审人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="advaners"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="nexterDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="nexter"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>


        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>进项票明细</span></div>
                        <div>
                            <button type="button" onclick="choose_piao_chu_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从进项票明细选择</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>发票号码</th>
                            <th>发票类型</th>
                            <th>开票日期</th>
                            <th>材料种类</th>
                            <th>不含税金额</th>
                            <th>税率(%)</th>
                            <th>税额(元)</th>
                            <th>价税合计(元)</th>
                            <th>付款状态</th>
                            <th>累计已付款金额</th>
                            <th>待付款金额</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
                <div class="suminfo">
                    <span>合计(元)：</span>
                    <span>不含税金额(<span id="sum_fee">0</span>)</span>
                    <span>税额(<span id="sum_listratefee">0</span>)</span>
                    <span>价税合计(<span id="sum_listmoney">0</span>)</span>
                    <span>累计已付款金额(<span id="sum_yifufee">0</span>)</span>
                    <span>待付款金额(<span id="sum_daifufee">0</span>)</span>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    var param_isturn = getQueryVariable('isturn') || 0;
    if(param_isturn > 0){
        $("#billzhan").remove();
        $("#billtutn").remove();
        $("#billsub").attr('isshen', 1);
    }
    var param_billyushen = getQueryVariable('billyushen') || 0;
    if(param_billyushen == 2){
        $("#billyushen").addClass('layui-hide');
    }


    $("#commentForm").validate({
        rules: {
            name: {required: true},
            number: {required: true},
            money: {required: true},
        },
        messages:{
            name: {required: "请输入"},
            number: {required: "请输入"},
            money: {required: "请输入"},
        }
    });


    function choose_dealcom(){
        var lianer = get_choose_val('lianer') || '';
        if(!lianer){
            facade.error('请先选择销售方联系人');return false;
        }
        openPage('../public/choose_dealcom.php?dom=gonger&page=contract_chu'+'&lianer='+encodeURI(lianer),'80%','80%')
    }


    function choose_piao_chu_list(obj){
        // var lianer = get_choose_val('lianer') || '';
        // if(!lianer){
        //     facade.error('请先选择销售方联系人');return false;
        // }
        var gonger = get_choose_val('gonger') || '';
        if(!gonger){
            facade.error('请先选择供应方(乙方)');return false;
        }
        var domkey = $(obj).parents('tr').index();
        openPage('../public/choose_piao_chu_list.php?page=contract_chu&domkey='+domkey+'&xiaoer='+encodeURI(gonger),'80%','80%');
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();
        facade.ajax({type: 'get', url: ask_flowdata_edit_contract_chu, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                    if(data.advaners_info.length > 0){
                        tan_tags_init('#advaners', data.advaners_info);
                    }
                }
                if(data.isnext){
                    $("#nexterDiv").removeClass('layui-hide');
                    if(data.nexter_info.length > 0){
                        tan_tags_init('#nexter', data.nexter_info);
                    }
                }
                if(data.flowrelas_info.length > 0){
                    set_choose_btn_more('flowrelas',data.flowrelas_info);
                }



                $("#name").val(data.name);
                $("#number").val(data.number);
                $("#gonger").val(data.gonger);
                $("#myer").val(data.myer);
                $("#henum").val(data.henum);
                $("#qiandt").val(data.qiandt);
                $("#money").val(data.money);
                $("#rate").val(data.rate);
                $("#explain").val(data.explain);

                if(data.lianer){
                    set_choose_btn('lianer',data.lianer,data.lianer);
                    $("#liantel").val(data.liantel);
                }
                if(data.gonger){
                    set_choose_btn('gonger',data.gonger,data.gonger);
                }

                if(data.caier_info.length > 0){
                    tan_tags_init('#caier', data.caier_info);
                }

                var state_str = '<option value="">请选择</option>';
                var state = res.data.state;
                for (var key in state){
                    if(data.state == key){
                        state_str += '<option value="'+key+'" selected>'+state[key]+'</option>';
                    }else{
                        state_str += '<option value="'+key+'">'+state[key]+'</option>';
                    }
                }
                $("#state").html(state_str);

                var hetype_str = '<option value="">请选择</option>';
                var hetype = res.data.hetype;
                for (var key in hetype){
                    if(data.hetype == key){
                        hetype_str += '<option value="'+key+'" selected>'+hetype[key]+'</option>';
                    }else{
                        hetype_str += '<option value="'+key+'">'+hetype[key]+'</option>';
                    }

                }
                $("#hetype").html(hetype_str);


                xmselect_dan('caitype', res.data.caitype, data.caitype, function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#caitype").find('xm-select').removeClass('error');
                    }else{
                        $("#caitype").find('xm-select').addClass('error');
                    }
                });

                xmselect_dan('qianpiaotype', res.data.qianpiaotype, data.qianpiaotype);








                short_table_opt_func('', 1);


                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" readonly piaoid="'+list[key].piaoid+'" piaolistid="'+list[key].piaolistid+'" class="layui-input nums disabled" value="'+list[key].nums+'" disabled></td>';
                        str += '<td><input type="text" class="layui-input qianpiaotype_name disabled" value="'+list[key].qianpiaotype_name+'" disabled></td>';
                        str += '<td><input type="text" class="layui-input kaidt disabled" value="'+list[key].kaidt+'" disabled></td>';
                        str += '<td><input type="text" class="layui-input cailei disabled" value="'+list[key].cailei+'" disabled></td>';
                        str += '<td><input type="number" class="layui-input fee disabled" value="'+list[key].fee+'" disabled></td>';
                        str += '<td><input type="number" class="layui-input rate disabled" value="'+list[key].rate+'" disabled></td>';
                        str += '<td><input type="number" class="layui-input listratefee disabled" value="'+list[key].listratefee+'" disabled></td>';
                        str += '<td><input type="number" class="layui-input listmoney disabled" value="'+list[key].listmoney+'" disabled></td>';
                        str += '<td><input type="text" class="layui-input statename disabled" value="'+list[key].statename+'" disabled></td>';
                        str += '<td><input type="number" class="layui-input yifufee disabled" value="'+list[key].yifufee+'" disabled></td>';
                        str += '<td><input type="number" class="layui-input daifufee disabled" value="'+list[key].daifufee+'" disabled></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }
                update_refresh_row_func();










                initParseMoney();
                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });

        form.on('select(hetype)', function(data){
            if(data.value == ''){
                $('#hetype_error').show().text('请选择');
                $('#hetype_error').parent().find('input').addClass('select_error');
            }else{
                $('#hetype_error').hide();
                $('#hetype_error').parent().find('input').removeClass('select_error');
            }
        });

        form.on('select(state)', function(data){
            if(data.value == ''){
                $('#state_error').show().text('请选择');
                $('#state_error').parent().find('input').addClass('select_error');
            }else{
                $('#state_error').hide();
                $('#state_error').parent().find('input').removeClass('select_error');
            }
        });



        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.isshen = $(this).attr('isshen') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');


            data.field.lianer = get_choose_val('lianer');
            data.field.gonger = get_choose_val('gonger');
            data.field.caier = tan_tags_getvalue("#caier", 'dept');

            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();


                if(data.field.state == ''){
                    $('#state_error').show().text('请选择');
                    $('#state_error').parent().find('input').addClass('select_error');
                    validRes = false;
                }else{
                    $('#state_error').hide();
                    $('#state_error').parent().find('input').removeClass('select_error');
                }

                if(!data.field.caitype){
                    $("#caitype").find('xm-select').addClass('error');
                    validRes = false;
                }else{
                    $("#caitype").find('xm-select').removeClass('error');
                }


                if(data.field.hetype == ''){
                    $('#hetype_error').show().text('请选择');
                    $('#hetype_error').parent().find('input').addClass('select_error');
                    validRes = false;
                }else{
                    $('#hetype_error').hide();
                    $('#hetype_error').parent().find('input').removeClass('select_error');
                }

                formValid('lianer');
                if(data.field.lianer == undefined || !data.field.lianer){
                    validRes = false;
                }

                formValid('gonger');
                if(data.field.gonger == undefined || !data.field.gonger){
                    validRes = false;
                }

                if(!data.field.caier){
                    $("#caier").parents('.tanxian').addClass('tanerror');
                    $('#caier_tanerror').text('请选择');
                    validRes = false;
                }else{
                    $("#caier").parents('.tanxian').removeClass('tanerror');
                    $('#caier_tanerror').text('');
                }

                if(!validRes){return false;}
            }


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var piaoid = $(this).find('.nums').attr('piaoid');
                    var piaolistid = $(this).find('.nums').attr('piaolistid');

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        var chongKey = check_chongfu(i, piaolistid);
                        if(chongKey){
                            throw new Error('第'+chongKey+'行'+'与第'+index+'行，发票号码重复');
                        }
                    }

                    list.push({
                        piaoid:piaoid,
                        piaolistid:piaolistid,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);

            public_flow_submit(data.field, ask_flowdata_add_contract_chu, ask_flowdata_edit_contract_chu, $(this).attr('but'));
            return false;
        });

    });


    //检测重复
    function check_chongfu(index, piaolistid){
        try {
            $('#list_list tr').each(function (i) {
                var cur_piaolistid = $(this).find('.nums').attr('piaolistid');
                if(index != i){
                    if(piaolistid == cur_piaolistid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }


    function choose_lianer_delete() {
        $("#gonger").html('');
        $("#list_list").html('');
    }
    function choose_gonger_delete() {
        $("#list_list").html('');
    }



    function jisuan_count_xiang() {
        var sum_fee = 0;
        var sum_listratefee = 0;
        var sum_listmoney = 0;
        var sum_yifufee = 0;
        var sum_daifufee = 0;
        $('#list_list tr').each(function (i) {
            var fee = parseValidNum($(this).find('.fee').val());
            var listratefee = parseValidNum($(this).find('.listratefee').val());
            var listmoney = parseValidNum($(this).find('.listmoney').val());
            var yifufee = parseValidNum($(this).find('.yifufee').val());
            var daifufee = parseValidNum($(this).find('.daifufee').val());

            sum_fee = parseFloat(sum_fee) + parseFloat(fee);
            sum_listratefee = parseFloat(sum_listratefee) + parseFloat(listratefee);
            sum_listmoney = parseFloat(sum_listmoney) + parseFloat(listmoney);
            sum_yifufee = parseFloat(sum_yifufee) + parseFloat(yifufee);
            sum_daifufee = parseFloat(sum_daifufee) + parseFloat(daifufee);
        });

        $("#sum_fee").text(parseFormatNum(sum_fee));
        $("#sum_listratefee").text(parseFormatNum(sum_listratefee));
        $("#sum_listmoney").text(parseFormatNum(sum_listmoney));
        $("#sum_yifufee").text(parseFormatNum(sum_yifufee));
        $("#sum_daifufee").text(parseFormatNum(sum_daifufee));
    }




    function update_refresh_row_func() {
        render_date();
        jisuan_count_xiang();

        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });

        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }
</script>
</body>
</html>
